package logic

import (
	"context"
	"github.com/olivere/elastic/v7"
	"zg6/zg6_month/goodssrv/service/es"

	"zg6/zg6_month/goodssrv/goodssrv"
	"zg6/zg6_month/goodssrv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchGoodsLogic {
	return &SearchGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchGoodsLogic) SearchGoods(in *goodssrv.SearchGoodsReq) (*goodssrv.SearchGoodsRes, error) {
	// todo: add your logic here and delete this line
	query := elastic.NewMatchQuery("GoodsName", in.Title)

	high := elastic.NewHighlight().Fields(
		elastic.NewHighlighterField("GoodsName").PreTags("<span 'style:color=res'>").PostTags("</span>"),
	)
	res, err := es.SearchGoods(query, high, int(in.Page))
	if err != nil {
		return nil, err
	}
	return &goodssrv.SearchGoodsRes{
		Goods: res,
	}, nil
}
